// 获取DOM元素
const video = document.getElementById('video');
const captureButton = document.getElementById('capture');
const canvas = document.getElementById('canvas');

// 获取用户媒体流
navigator.mediaDevices.getUserMedia({
		video: true
	})
	.then((stream) => {
		video.srcObject = stream;
	})
	.catch((err) => {
		console.error("Error accessing the camera: ", err);
	});

// 捕获图像
captureButton.addEventListener('click', () => {
	const context = canvas.getContext('2d');
	canvas.width = video.videoWidth;
	canvas.height = video.videoHeight;
	context.drawImage(video, 0, 0, canvas.width, canvas.height);

	// 显示捕获的图像
	canvas.style.display = 'block';

	// 你可以在这里添加代码来保存或处理捕获的图像
	// 例如，将图像转换为Data URL
	const imageDataUrl = canvas.toDataURL('image/png');
	console.log(imageDataUrl);
});